<?php

include_once "regsettings.php";
include_once "regfunctions.php";

// includes
include_once REG_CONNECT_FILE;
include_once REG_QUERY_FILE;

	if (isset($_REQUEST['need'])) {
		$need = $_REQUEST['need'];
	}
	else {
		$need = "password";
	} 
	
	if (isset($_REQUEST['username'])) {
		$username = $_REQUEST['username'];
	}
	else {
		$username = "";
	}

	if (isset($_REQUEST['email'])) {
		$email = $_REQUEST['email'];
	}
	else {
		$email = "";
	}	
	

	
if ($need == 'username' OR $need == 'Get Username'){
	if(!empty($email)){
		$username = lookup_value('username',REG_USER_TABLE," email = '$email'");	
		$item = REG_MSG_USERNAME . $username;	
		$dosend = true;
	}	
}	
else{
	if(!empty($username)){

		//!!!!MAKE username Safe HEre 
		$temp_pass = generateRandStr(10);

		$qry_set_temppw = "UPDATE " . REG_USER_TABLE . " SET temp_pass = '$temp_pass' WHERE username = '$username'";
		do_query($qry_set_temppw);
		$qry_set_temptime = "UPDATE " . REG_USER_TABLE . " SET temp_expire_date = DATE_ADD(Now(), INTERVAL 1 DAY) WHERE username = '$username'";
		do_query($qry_set_temptime);
		$item = REG_MSG_PASSWORD . $temp_pass;
	
		$dosend = true;
	
	}
}

if($dosend){

	$body = "<body style='margin: 10px;'>
	<div style='width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;'>
	<div align='center'><img src='images/phpmailer.gif' style='height: 90px; width: 340px'></div><br>
	<br>"
	. $item . "

	</div>
	</body>";


	//error_reporting(E_ALL);
	error_reporting(E_STRICT);

	date_default_timezone_set('America/Toronto');

	require_once(REG_MAILER_DIR . 'class.phpmailer.php');
	//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

	$mail             = new PHPMailer();

	//$body             = file_get_contents('contents.html');
	$body             = eregi_replace("[\]",'',$body);

	require_once "mail_settings.php";

	if(!$mail->Send()) {
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	
	  //echo "Message sent!";
	}
}
else{
	header("Location: lostlogin.php");
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Retrieve Account Info</title>
</head>
<body>

<h2><?php print $item ?> has been sent to the email associated with this account </h2>
<h3>
<a href='login.php'> Return to Login page </a>
</h3>

</body>
</html>

